package top.yueyazhui.learn_shiro.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import top.yueyazhui.learn_shiro.model.Response;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author yueyazhui
 * @Date 2023/2/18
 */
@Controller
public class LoginController {

    @GetMapping(value = "login", produces = "text/html;charset=utf-8")
    public String login() {
        return "loginForm";
    }

    @PostMapping(value = "login", produces = "text/html;charset=utf-8")
    public String login(String username, String password, Model model) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
        // 开启 RememberMe
        usernamePasswordToken.setRememberMe(true);
        try {
            // 执行登录
            Subject subject = SecurityUtils.getSubject();
            subject.login(usernamePasswordToken);
            List list = subject.getPrincipals().asList();
            for (Object o : list) {
                System.out.println("o = " + o);
            }
        } catch (AuthenticationException e) {
            request.setAttribute("errorMsg1", e.getMessage());
            model.addAttribute("errorMsg2", e.getMessage());
            return "forward:/loginForm";
        }
        // 登录成功，重定向到 index
        return "redirect:/index";
    }

//    /**
//     * Shiro 提供的表单登录，这个接口提供了两个功能
//     * 1、提供登录页面
//     * 2、处理登录请求
//     * @param model
//     * @return
//     */
//    @RequestMapping(value = "login", produces = "text/html;charset=utf-8")
//    public String login(Model model) {
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
//        // 获取登录失败的异常信息
//        String shiroLoginFailure = (String) request.getAttribute("shiroLoginFailure");
//        if(UnknownAccountException.class.getName().equals(shiroLoginFailure) || IncorrectCredentialsException.class.getName().equals(shiroLoginFailure)) {
//            request.setAttribute("errorMsg1", "用户名或密码输入错误");
//            model.addAttribute("errorMsg2", "用户名或密码输入错误");
//        }
//        return "loginForm";
//    }

    @RequestMapping("index")
    public String index() {
        return "index";
    }

    @RequestMapping("loginForm")
    public String loginForm() {
        return "loginForm";
    }

    @GetMapping("logout")
    @ResponseBody
    public Response logout() {
        try {
            SecurityUtils.getSubject().logout();
            return Response.success("注销成功");
        } catch (Exception e) {
            return Response.error("注销失败");
        }
    }
}
